function* gen() {
}

function generatorToAsync(generatorFn) {
  return function() {
    const gen = generatorFn.apply(this, arguments)

    // 返回一个 promise
    return new Promise((resolve, reject) => {
      function go(key, arg) {
        let res 
        try {
          res = gen[key](arg)
        } catch (e) {
          return reject(error)
        }

        // 解构获得value 和 done
        const { value, done } = res
        if (done) {
          // 如果done为true，说明走完了，进行resolve(value)
          return resolve(value)
        } else {
          // 如果done为false，说明没走完，还得继续走

          // value有可能是：常量，Promise，Promise有可能是成功或者失败
          return Promise.resolve(value).then(val => go('next', val), err => go('throw', err))
        }
      }

      go("next") // 第一次执行
    })
  }
}

async function asyncFn() {
  const num1 = await fn(1)
  console.log(num1) // 2
  const num2 = await fn(num1)
  console.log(num2) // 4
  const num3 = await fn(num2)
  console.log(num3) // 8
  return num3
}
const asyncRes = asyncFn()
console.log(asyncRes) // Promise
asyncRes.then(res => console.log(res)) // 8